import 'package:flutter/material.dart';
import 'package:netflix_social/constants/app_constants.dart';
import 'package:netflix_social/providers/auth_provider.dart';
import 'package:netflix_social/providers/video_provider.dart';
import 'package:netflix_social/providers/social_provider.dart';
import 'package:netflix_social/theme/app_theme.dart';
import 'package:provider/provider.dart';

class SplashScreen extends StatefulWidget {
  const SplashScreen({Key? key}) : super(key: key);

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _fadeAnimation;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1500),
    );
    _fadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(_animationController);
    
    _animationController.forward();
    
    // 检查登录状态并初始化数据
    _checkLoginStatusAndInitialize();
  }

  Future<void> _checkLoginStatusAndInitialize() async {
    // 获取提供者实例
    final authProvider = Provider.of<AuthProvider>(context, listen: false);
    final videoProvider = Provider.of<VideoProvider>(context, listen: false);
    final socialProvider = Provider.of<SocialProvider>(context, listen: false);
    
    // 检查登录状态
    await authProvider.checkLoginStatus();
    
    // 初始化视频和社交数据
    await videoProvider.initialize();
    await socialProvider.initialize();
    
    // 延迟一下，让动画完成
    await Future.delayed(const Duration(milliseconds: 2000));
    
    // 根据登录状态导航到不同页面
    if (authProvider.isLoggedIn) {
      // 如果已登录，导航到主页
      if (mounted) {
        Navigator.of(context).pushReplacementNamed(AppConstants.homeRoute);
      }
    } else {
      // 如果未登录，导航到登录页
      if (mounted) {
        Navigator.of(context).pushReplacementNamed(AppConstants.loginRoute);
      }
    }
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppTheme.backgroundColor,
      body: Center(
        child: FadeTransition(
          opacity: _fadeAnimation,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // 应用图标
              Container(
                width: 120,
                height: 120,
                decoration: BoxDecoration(
                  color: AppTheme.netflixRed,
                  borderRadius: BorderRadius.circular(20),
                ),
                child: const Center(
                  child: Icon(
                    Icons.movie_filter,
                    color: Colors.white,
                    size: 60,
                  ),
                ),
              ),
              const SizedBox(height: 24),
              // 应用名称
              Text(
                AppConstants.appName,
                style: Theme.of(context).textTheme.displayLarge,
              ),
              const SizedBox(height: 8),
              Text(
                AppConstants.appSlogan,
                style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                  color: AppTheme.secondaryTextColor,
                ),
              ),
              const SizedBox(height: 48),
              // 加载指示器
              const CircularProgressIndicator(
                valueColor: AlwaysStoppedAnimation<Color>(AppTheme.netflixRed),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
